﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ListItemEditor.UI
{
    public partial class SelectWebListForm : Form
    {
        public class WebListData
        {
            public string WebUrl { get; set; }
            public List<string> ListNames { get; set; }
        }

        private GridViewDataSourceSettings _currentSettings;

        public GridViewDataSourceSettings CurrentSettings
        {
            get
            {
                if (_currentSettings == null) _currentSettings = new GridViewDataSourceSettings();
                if (_currentSettings.UserName == null) _currentSettings.UserName = string.Empty;
                if (_currentSettings.Password == null) _currentSettings.Password = string.Empty;
                return _currentSettings;
            }
            set
            {
                _currentSettings = value;
            }
        }
        public WebListData CurrentWebListData { get; set; }
        public bool UseWebSvc { get; set; }

        public string SelectedListName
        {
            get { return this.lvLists.SelectedItems == null | this.lvLists.SelectedItems.Count == 0 ? null : this.lvLists.SelectedItems[0].Text; }
        }

        public SelectWebListForm()
        {
            InitializeComponent();
        }

        private void SelectWebListForm_Load(object sender, EventArgs e)
        {
            btnOK.Enabled = false;
            btnCredentials.Visible = this.UseWebSvc;
            this.SetControlsFromWebListData();
        }

        private void btnLoadLists_Click(object sender, EventArgs e)
        {
            try
            {
                IListDataProvider provider = this.UseWebSvc ? (IListDataProvider)new ListDataProviderWebSvc() : (IListDataProvider)new ListDataProvider();
                CurrentSettings.WebUrl = this.txtWebUrl.Text;

                CurrentWebListData = new WebListData()
                {
                    WebUrl = CurrentSettings.WebUrl,
                    ListNames = provider.GetListNamesFromWeb(CurrentSettings)
                };
                SetControlsFromWebListData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message);
            }
        }

        private void lvLists_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnOK.Enabled = !(lvLists.SelectedIndices == null || lvLists.SelectedIndices.Count == 0);
        }

        private void SetControlsFromWebListData()
        {
            if (this.CurrentWebListData != null)
            {
                this.txtWebUrl.Text = this.CurrentWebListData.WebUrl;
                this.lvLists.Items.Clear();

                if (this.CurrentWebListData.ListNames != null)
                    this.lvLists.Items.AddRange(this.CurrentWebListData.ListNames.Select(l => new ListViewItem(l)).ToArray());
            }
        }

        private void btnCredentials_Click(object sender, EventArgs e)
        {
            List<string> values = new List<string>() { this.CurrentSettings.UserName, this.CurrentSettings.Password };
            DialogResult res = Prompt.ShowMultiplePrompt(this, "Enter credentials", values, new List<string>() { "username", "password" }, new List<bool>() { false, true });
            if (res != DialogResult.OK) return;
            this.CurrentSettings.UserName = values[0];
            this.CurrentSettings.Password = values[1];
        }

        private void lvLists_DoubleClick(object sender, EventArgs e)
        {
            if (this.lvLists.SelectedItems != null && this.lvLists.SelectedItems.Count > 0)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}